package com.example.demo.controller;


import java.util.List;
import java.util.Map;

import org.apache.catalina.User;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.pojo.UserInfo;
import com.example.demo.service.UserInfoService;
import com.example.demo.utils.ServerEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;


@Controller
@RequestMapping("/")
public class UserInfoController {
	
@Autowired
private UserInfoService userInfoservice;
@Autowired
private DiscoveryClient  discoveryClient; 
@Autowired
private EurekaClient eurekaClient;



@RequestMapping("")
public String index(Map<String,String> map){	
	map.put("index", "Hello word!");
	return "/index";
}


@RequestMapping("userInfo")
 public String getUserInfo(Map<String,Object> map,HttpServletRequest request){
	  
	 int pageNum = request.getParameter("pageNum")==null?0: Integer.parseInt(request.getParameter("pageNum"));
	 int numPerPage = request.getParameter("numPerPage")==null?20: Integer.parseInt(request.getParameter("numPerPage"));
	 PageHelper.startPage(pageNum, numPerPage);
	 List<Map<String,Object>> userInfo = userInfoservice.getUserAll(); 
	 PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(userInfo);
	 map.put("userInfo", pageInfo.getList());
	 map.put("page", pageInfo.getPrePage());
	 map.put("pageSize", pageInfo.getPageSize());
	 map.put("total", pageInfo.getTotal());
     return "/Dwz/user/userInfo";
     
 }


@GetMapping("eureka-instance")
@ResponseBody
public String getEureka(){
    
	InstanceInfo instance = eurekaClient.getNextServerFromEureka("SPRING-CLOUD-PROVIDER", false);
	return  instance.getHomePageUrl();
}

@GetMapping("/instance-info")
@ResponseBody
public ServiceInstance showInfo() {
  ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
  return localServiceInstance;
}

}
